package ru.leo.tennis.tools.comhelper;

import java.io.IOException;
import java.io.InputStream;
   
public class CommPortReceiver extends Thread {  
   
    private InputStream in;  
    private Protocol protocol;  
   
    public CommPortReceiver(InputStream in) {  
        this.in = in;
        protocol = new ProtocolImpl();
    }  
      
    public void run() {  
        try {  
            int b;
            
            while(true) {  
                  
                // if stream is not bound in.read() method returns -1
            	
                while((b = in.read()) != -1) {
                    protocol.onReceive((byte)b);  
                }  
                protocol.onStreamEnds();  
                  
                // wait 10ms when stream is broken and check again  
                sleep(10);  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (InterruptedException e) {  
            e.printStackTrace();  
        }   
    }  
}  